package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.dto.FollowBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApFollowBehavior;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP关注行为表 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-17
 */
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements IApFollowBehaviorService {
    @Autowired
    private IApBehaviorEntryService entryService;
    @Override
    public void saveFollow(FollowBehaviorDto followBehaviorDto) {
        //保存实体
        EntryDto dto = new EntryDto();
        dto.setUserId(followBehaviorDto.getUserId());
        dto.setEquipmentId(followBehaviorDto.getEquipmentId());
        ApBehaviorEntry entry = entryService.saveEntry(dto);
        //保存关注行为
        //构建条件查询是否存在
        LambdaQueryWrapper<ApFollowBehavior> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApFollowBehavior::getEntryId,entry.getId());
        wrapper.eq(ApFollowBehavior::getFollowId,followBehaviorDto.getFollowId());
        ApFollowBehavior behavior = getOne(wrapper);
        if (behavior!=null) {
            behavior.setOperation(followBehaviorDto.getOperation());
        }else {
            behavior = new ApFollowBehavior();
            behavior.setEntryId(entry.getId());
            behavior.setFollowId(followBehaviorDto.getFollowId());
            behavior.setCreatedTime(new Date());
            behavior.setOperation(followBehaviorDto.getOperation());
        }
        saveOrUpdate(behavior);
    }
}
